﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Text.RegularExpressions;

namespace SupplyChainManagement
{
    /// <summary>
    /// The form used to edit an inventory
    /// </summary>
    public partial class InventoryEditForm : Form
    {
        private InventoryForm mainForm;
        private string invId;
        
        public InventoryEditForm()
        {
            InitializeComponent();
        }

        public InventoryEditForm(InventoryForm form, DataGridViewCellCollection cells)
        {
            mainForm = form;         
            InitializeComponent();
            txtID.Text = invId = cells[0].Value.ToString();
            txtName.Text = cells[1].Value.ToString();
            txtMin.Text = cells[2].Value.ToString();
            txtMax.Text = cells[3].Value.ToString();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //check if any of the fields are empty
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtMax.Text) || string.IsNullOrEmpty(txtMin.Text))
                MessageBox.Show("Data is not complete");
            //check if price only contains numbers
            else if (Regex.IsMatch(txtMax.Text, @"^[0-9,-\.]+$") && Regex.IsMatch(txtMin.Text, @"^[0-9,-\.]+$"))
            {
                EditEntry();
                //subject.Update(invId);
                MessageBox.Show("Success!");
                    
                //move back to main form
                this.Close();
                mainForm.LoadDatabase();
                mainForm.Enabled = true;
                mainForm.Activate();
        }
            else MessageBox.Show("Data is not valid.");            
            
        }

        /// <summary>
        /// Updates the database with the updated value
        /// </summary>
        /// <returns></returns>
        private void EditEntry()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();

                //update the inventory with the new data
                OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                sqlCommand.CommandText = "update inventory set inv_name='" + txtName.Text + "', inv_max='" + txtMax.Text +
                    "', inv_min='" + txtMin.Text + "' where inv_id='" + invId + "'";
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Dispose();
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        private void EditForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainForm.Enabled = true;
            mainForm.Activate();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            mainForm.Enabled = true;
            mainForm.Activate();
        }

    }
}
